package com.ai.service.made.impl;


import com.ai.common.CommonResponse;
import com.ai.entity.cameraFunctions.CameraFunctionID;
import com.ai.entity.cameraFunctions.regions.RegionsBO;
import com.ai.entity.cameraFunctions.regions.RegionsPO;
import com.ai.entity.made.CoordinateBO;
import com.ai.entity.made.LineBO;
import com.ai.entity.made.camera.SwithCameraFunctionRegionsAlgorithmBO;
import com.ai.mapper.made.RegionsMapper;
import com.ai.service.made.RegionsService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr.Chen
 * @since 2020-08-14
 */
@Service
public class RegionsServiceImpl extends ServiceImpl<RegionsMapper, RegionsPO> implements RegionsService {

    @Override
    public List<SwithCameraFunctionRegionsAlgorithmBO> getSpots(int cameraFunctionsId) {
        List<SwithCameraFunctionRegionsAlgorithmBO> spots = new ArrayList<>();
        List<RegionsPO> regionsPOList = list(new QueryWrapper<RegionsPO>().eq("camera_functions_id", cameraFunctionsId));
        if (regionsPOList.size() > 0) {
            regionsPOList.forEach(regionsPO -> {
                SwithCameraFunctionRegionsAlgorithmBO bo = new SwithCameraFunctionRegionsAlgorithmBO();
                bo.setId(regionsPO.getId());
                bo.setNickname(regionsPO.getNickname());
                bo.setCode(regionsPO.getCode());
                bo.setSpots(JSONArray.parseArray(regionsPO.getSpots(), CoordinateBO.class));
                bo.setLinePoints(JSONArray.parseArray(regionsPO.getLinePoints(), LineBO.class));
                spots.add(bo);
            });
        } else {
            SwithCameraFunctionRegionsAlgorithmBO bo = new SwithCameraFunctionRegionsAlgorithmBO();
            bo.init();
            spots.add(bo);
        }
        return spots;
    }

    @Override
    public CommonResponse regionsList(CameraFunctionID cameraFunctionID) {
        if (StringUtils.isEmpty(cameraFunctionID.getCameraFunctionsId())) {
            return CommonResponse.error("功能不能为空");
        }
        List<RegionsPO> list = list(new QueryWrapper<RegionsPO>().eq("camera_functions_id", cameraFunctionID.getCameraFunctionsId()));
        if (list.size() <= 0) {
            RegionsPO regionsPO = new RegionsPO();
            regionsPO.setId(0);
            regionsPO.setCameraFunctionsId(cameraFunctionID.getCameraFunctionsId());
            regionsPO.setCode(100);
            regionsPO.setNickname("全屏");
            SwithCameraFunctionRegionsAlgorithmBO swithCameraFunctionRegionsAlgorithmBO = new SwithCameraFunctionRegionsAlgorithmBO().init();
            regionsPO.setSpots(JSONObject.toJSONString(swithCameraFunctionRegionsAlgorithmBO.getSpots()));
            list.add(regionsPO);
        }
        List<RegionsBO> ar = new ArrayList<>();
        list.forEach(v -> {
            RegionsBO bo = new RegionsBO();
            BeanUtils.copyProperties(v, bo);
            bo.setSpots(JSON.parseArray(v.getSpots(), CoordinateBO.class));
            bo.setLinePoints(JSON.parseArray(v.getLinePoints(), LineBO.class));
            ar.add(bo);
        });
        return CommonResponse.ok(ar);
    }

    @Override
    public CommonResponse binding(RegionsBO regionsBO) {
        Integer id = regionsBO.getId();
        if (id == null) {
            return CommonResponse.error("修改失败");
        }
        if (id == 0) {
            return CommonResponse.error("默认区域无法修改，新加一个区域将自动删除此区域");
        }
        if (StringUtils.isEmpty(regionsBO.getSpots())) {
            return CommonResponse.error("区域不能为空");
        }
        if (regionsBO.getSpots().size() < 3) {
            return CommonResponse.error("请上传至少3个点位坐标");
        }
        RegionsPO regionsPO = new RegionsPO();
        BeanUtils.copyProperties(regionsBO, regionsPO);
        regionsPO.setSpots(JSONObject.toJSONString(regionsBO.getSpots()));
        regionsPO.setLinePoints(JSONObject.toJSONString(regionsBO.getLinePoints()));
        regionsPO.setId(id);
        if (updateById(regionsPO)) {
            regionsPO.setId(null);
            return CommonResponse.ok("提交成功");
        }
        return CommonResponse.error("提交失败");
    }

    @Override
    public CommonResponse unbound(RegionsBO regionsBO) {
        Integer id = regionsBO.getId();
        if (id == null) {
            return CommonResponse.error("删除失败");
        }
        if (id == 0) {
            return CommonResponse.error("默认区域无法删除，新加一个区域将自动删除此区域");
        }
        if (removeById(id)) {
            return CommonResponse.ok("删除成功");
        }
        return CommonResponse.error("删除失败");
    }

    @Override
    public CommonResponse add(RegionsBO regionsBO) {
        String nickname = regionsBO.getNickname();
        Integer cameraFunctionsId = regionsBO.getCameraFunctionsId();
        if (cameraFunctionsId == null || cameraFunctionsId == 0) {
            return CommonResponse.error("功能不能为空");
        }
        if (StringUtils.isEmpty(nickname)) {
            return CommonResponse.error("标签昵称不能为空");
        }
        if (count(new QueryWrapper<RegionsPO>().eq("nickname", nickname).eq("camera_functions_id", cameraFunctionsId)) > 0) {
            return CommonResponse.error("提交失败,标签昵称不能重复");
        }
        RegionsPO regionsPO = new RegionsPO();
        SwithCameraFunctionRegionsAlgorithmBO swithCameraFunctionRegionsAlgorithmBO = new SwithCameraFunctionRegionsAlgorithmBO().init();
        regionsPO.setSpots(JSONObject.toJSONString(swithCameraFunctionRegionsAlgorithmBO.getSpots()));
        regionsPO.setCameraFunctionsId(cameraFunctionsId);
        regionsPO.setCode(100);
        regionsPO.setNickname(nickname);
        if (save(regionsPO)) {
            regionsPO.setId(null);
            return CommonResponse.ok("提交成功");
        }
        return CommonResponse.error("提交失败");
    }
}
